import 'package:bruno/bruno.dart';
import 'package:dart_date/dart_date.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/pages/site/widgets/statistics_time_unit_tab.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/time_formated.dart';
import 'package:yt_devops/utils/enums.dart';

class YtTimePicker extends StatefulWidget {
  ///pickerMode  选择的日期类型
  ///
  /// 可选择年、月、日、累计
  final DateTimePickerMode pickerMode;

  ///initialDateTime  初始日期
  final DateTime? initialDateTime;

  /// onChange 日期切换时的回调
  final Function(DateTime, StatisticUnitStatusType? timeTabMode)? onChange;
  const YtTimePicker(
      {required this.pickerMode,
      this.initialDateTime,
      this.onChange,
      super.key});

  @override
  State<YtTimePicker> createState() => _YtTimePickerState();
}

class _YtTimePickerState extends State<YtTimePicker> {
  late DateTime currentDateTime;

  late DateTimePickerMode _pickerMode;

  String get timeFormateString => formteTimeStringByMode(currentDateTime);

  StatisticUnitStatusType _timeTabType = StatisticUnitStatusType.day;

  String pickerTitle = '';

  formteTimeStringByMode(
    DateTime time,
  ) {
    String formate = '';
    switch (_pickerMode) {
      case DateTimePickerMode.minute:
        formate = TimeFormated.minuteTimeFromated;
        break;

      case DateTimePickerMode.day:
        formate = TimeFormated.dateShowFromated;
        break;

      case DateTimePickerMode.month:
        formate = TimeFormated.monthShowFromated;
        break;

      default:
        formate = TimeFormated.yearShowFromated;
        break;
    }
    return time.format(formate);
  }

  @override
  void initState() {
    initTime();

    super.initState();
  }

  @override
  void didUpdateWidget(covariant YtTimePicker oldWidget) {
    if (oldWidget.initialDateTime != widget.initialDateTime) {
      // initTime();
      if (mounted) {
        setState(() {});
      }
    }
    super.didUpdateWidget(oldWidget);
  }

  initTime() {
    _pickerMode = widget.pickerMode == DateTimePickerMode.all
        ? DateTimePickerMode.day
        : widget.pickerMode;

    currentDateTime = widget.initialDateTime ?? DateTime.now();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // color: Colors.red,
      padding: EdgeInsets.only(bottom: 16),
      child: Column(children: [
        if (widget.pickerMode == DateTimePickerMode.all)
          StatisticsTimeUnitTab(
            _timeTabType ?? StatisticUnitStatusType.day,
            changeTimeTabHandler,
          ),
        Gaps.vGap14,
        if (_timeTabType != StatisticUnitStatusType.accumulate)
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () => _stepHanlder(-1),
                    child: Container(
                        width: 24.w,
                        height: 24.w,
                        decoration: BoxDecoration(
                            color: Colour.backgroundColor,
                            borderRadius: BorderRadius.circular(4.w)),
                        child: const Icon(Icons.chevron_left)),
                  ),
                  InkWell(
                    onTap: _showTimePicker,
                    child: Container(
                      height: 24.h,
                      padding: EdgeInsets.only(left: 12.w, right: 15.w),
                      decoration: BoxDecoration(
                          color: Colour.backgroundColor,
                          borderRadius: BorderRadius.circular(4.w)),
                      child: Row(
                        children: [
                          Text(
                            timeFormateString,
                            style: TextStyles.f14.mediumStyle,
                          ),
                          Image.asset(ImageAssets.arrowDown)
                        ],
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () => _stepHanlder(1),
                    child: Container(
                      width: 24.w,
                      height: 24.w,
                      decoration: BoxDecoration(
                        color: Colour.backgroundColor,
                        borderRadius: BorderRadius.circular(4.w),
                      ),
                      child: const Icon(Icons.chevron_right),
                    ),
                  ),
                ]),
          ),
      ]),
    );
  }

  changeTimeTabHandler(StatisticUnitStatusType type) {
    _timeTabType = type;
    switch (_timeTabType) {
      case StatisticUnitStatusType.day:
        _pickerMode = DateTimePickerMode.day;
        break;
      case StatisticUnitStatusType.month:
        _pickerMode = DateTimePickerMode.month;
        break;
      case StatisticUnitStatusType.year:
        _pickerMode = DateTimePickerMode.year;
        break;
      case StatisticUnitStatusType.accumulate:
        _pickerMode = DateTimePickerMode.year;
        break;
      default:
    }
    setState(() {});
    if (widget.onChange != null) {
      widget.onChange!(currentDateTime, _timeTabType);
    }
  }

  _stepHanlder(int num) {
    switch (_pickerMode) {
      case DateTimePickerMode.year:
        currentDateTime = currentDateTime.addYears(num);
        break;
      case DateTimePickerMode.month:
        currentDateTime = currentDateTime.addMonths(num);
        break;
      default:
        currentDateTime = currentDateTime.addDays(num);
        break;
    }
    if (mounted) {
      setState(() {});
    }
    if (widget.onChange != null) {
      widget.onChange!(currentDateTime, _timeTabType);
    }
  }

  _showTimePicker() {
    String format;

    switch (_pickerMode) {
      case DateTimePickerMode.year:
        format = 'yyyy${ResKey.selectYear.tr}';
        break;
      case DateTimePickerMode.month:
        format = 'yyyy${ResKey.selectYear.tr},MM${ResKey.selectMonth.tr}';
        break;
      case DateTimePickerMode.day:
        format =
            'yyyy${ResKey.selectYear.tr},MM${ResKey.selectMonth.tr},dd${ResKey.selectDay.tr}';
        break;
      case DateTimePickerMode.minute:
        format =
            'yyyy${ResKey.selectYear.tr},MM${ResKey.selectMonth.tr},dd${ResKey.selectDay.tr},HH${ResKey.selectHour.tr}:mm${ResKey.selectMinute.tr},';
        break;
      default:
        format = 'yyyy-MMMM-dd';
        break;
    }
    pickerTitle = timeFormateString;

    BrnDatePicker.showDatePicker(
      context,
      initialDateTime: currentDateTime,
      // maxDateTime:DateTime.now(),
      pickerMode: BrnDateTimePickerMode.date,
      pickerTitleConfig: BrnPickerTitleConfig(
        titleContent: pickerTitle,
        cancel: Text(
          ResKey.cancel.tr,
          style: TextStyle(
            color: Colour.lightTextColor,
            fontSize: 14.sp,
          ),
        ),
        confirm: Text(
          ResKey.sure.tr,
          style: TextStyle(
            color: Colour.primaryColor,
            fontSize: 14.sp,
          ),
        ),
      ),
      rootNavigator: true,
      dateFormat: format,
      onConfirm: (dateTime, list) {
        currentDateTime = dateTime;
        if (widget.onChange != null) {
          widget.onChange!(dateTime, _timeTabType);
        }
        if (mounted) {
          setState(() {});
        }
      },
      onChange: (dateTime, selectedIndex) {
        pickerTitle = formteTimeStringByMode(dateTime);

        setState(() {});
      },
    );
  }
}
